﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Windows.Forms;
using System.Text;
using System.Threading;
using TouchUI;

namespace MobileStore.WinPhone
{
    public class DetailScrollPanel : ScrollPanel
    {
        private Car car;
        private Bitmap[] images = new Bitmap[3];
        private Bitmap callButton;
        private int imageHeight;
        private int currentImage;
        private int animationOffset;
        private int animationLeft;
        private bool loadingImage;
        private int loadedImages = -1;

        public DetailScrollPanel(Car car, int screenFactor, Rectangle r)
        {
            this.car = car;
            this.images[0] = Common.Values.LoadBitmap("no.jpg", 400, 300);
            if(car.Code != "no")
            {
                Thread loadImageThread = new Thread(new ThreadStart(loadImage));
                loadImageThread.Start();
                loadingImage = true;
            }
            this.callButton = Common.Values.LoadBitmap("call.png", 30, 30);
            ScreenFactor = screenFactor;
            Rectangle = r;
            imageHeight = Rectangle.Width * images[0].Size.Height / images[0].Size.Width;
            ScrollHeight = 500 * screenFactor;
        }

        private void loadImage()
        {
            Bitmap image = Common.Values.LoadBitmap(new Uri(Common.Values.ImageUrl + car.Code + ".jpg"), 400, 300);
            loadedImages++;
            lock(this.images)
                this.images[0] = image;
            loadingImage = false;

            if(car.Code == "honda")
            {
                for(int i = 1; i < 3; i++)
                {
                    image = Common.Values.LoadBitmap(new Uri(Common.Values.ImageUrl + car.Code + i.ToString() + ".jpg"), 400, 300);
                    lock(this.images)
                        this.images[i] = image;
                    loadedImages++;
                }
            }
        }

        public override void Paint(Graphics g)
        {
            this.imageHeight = Rectangle.Width * images[0].Size.Height / images[0].Size.Width;
            lock(images)
            {
                if(animationLeft != 0)
                {
                    g.DrawImage(images[currentImage - animationLeft.CompareTo(0)], new Rectangle(animationLeft - Rectangle.Width * animationLeft.CompareTo(0),
                        Rectangle.Top + ScrollTop, Rectangle.Width, this.imageHeight),
                        new Rectangle(0, 0, images[0].Width, images[0].Height), GraphicsUnit.Pixel);
                    g.DrawImage(images[currentImage], new Rectangle(animationLeft, Rectangle.Top + ScrollTop, Rectangle.Width, this.imageHeight),
                        new Rectangle(0, 0, images[0].Width, images[0].Height), GraphicsUnit.Pixel);
                    animationLeft -= animationOffset;
                    if(Math.Abs(animationLeft) < Math.Abs(animationOffset))
                        animationLeft = 0;
                }
                else
                    g.DrawImage(images[currentImage], new Rectangle(Rectangle.Left, Rectangle.Top + ScrollTop, Rectangle.Width, this.imageHeight),
                        new Rectangle(0, 0, images[0].Width, images[0].Height), GraphicsUnit.Pixel);
            }
            if(loadingImage)
                g.DrawString(Strings.LoadingImagePleaseWait, Common.Values.Font, Common.Values.FontBrush, Rectangle.Left + 8 * ScreenFactor, Rectangle.Top + ScrollTop + 8 * ScreenFactor);

            for(int i = 0; i < loadedImages + 1; i++)
            {
                int x = (int)((Rectangle.Width - 8 * ScreenFactor * loadedImages) / 2 + 0.5) + i * 8 * ScreenFactor;
                if(i == currentImage)
                    g.FillEllipse(Common.Values.FontBrush, x, Rectangle.Top + ScrollTop + imageHeight + 4 * ScreenFactor, 4 * ScreenFactor, 4 * ScreenFactor);
                else
                    g.DrawEllipse(Common.Values.Pen, x, Rectangle.Top + ScrollTop + imageHeight + 4 * ScreenFactor, 4 * ScreenFactor, 4 * ScreenFactor);
            }

            int lineHeight = g.MeasureString("X", Common.Values.Font).ToSize().Height;
            //int imageHeight = this.imageHeight - lineHeight / 2;

            drawString(g, "Price:", 8, imageHeight + lineHeight);
            drawString(g, "$" + car.Price.ToString(), 48, imageHeight + lineHeight);
            drawString(g, "Color:", 8, imageHeight + lineHeight * 2);
            drawString(g, car.Color, 48, imageHeight + lineHeight * 2);
            drawString(g, "Year:", 8, imageHeight + lineHeight * 3);
            drawString(g, car.Year, 48, imageHeight + lineHeight * 3);
            drawString(g, "Miles:", 8, imageHeight + lineHeight * 4);
            drawString(g, (car.Km / 20).ToString(), 48 , imageHeight + lineHeight * 4);

            string s = "(999) 1234-1234";
            int phoneWidth = g.MeasureString(s, Common.Values.Font).ToSize().Width;
            g.DrawString(s, Common.Values.Font, Common.Values.FontBrush, Rectangle.Width - phoneWidth - 8 * ScreenFactor, Rectangle.Top + ScrollTop + imageHeight + lineHeight);
            if(Common.Values.TouchDevice)
                g.DrawImage(callButton, new Rectangle(Rectangle.Left + Rectangle.Width - callButton.Width - 8 * ScreenFactor, Rectangle.Top + ScrollTop + imageHeight + lineHeight * 2,
                    callButton.Width, callButton.Height), new Rectangle(0, 0, callButton.Width, callButton.Height), GraphicsUnit.Pixel);

            g.FillRectangle(Common.Values.FontBrush, 0, Rectangle.Top + ScrollTop + imageHeight + lineHeight * 6 - 4 * ScreenFactor, Rectangle.Width, lineHeight + 4 * ScreenFactor);
            g.DrawString("Equipment", Common.Values.Font, Common.Values.BackBrush, Rectangle.Left + 8 * ScreenFactor, Rectangle.Top + ScrollTop + imageHeight + lineHeight * 6 - 2 * ScreenFactor);

            drawString(g, "Air Bag", 8, imageHeight + lineHeight * 7);
            drawString(g, "Air Conditioning", 8, imageHeight + lineHeight * 8);
            drawString(g, "ABS", 8, imageHeight + lineHeight * 9);
            drawString(g, "Automatic Seatbelts", 8, imageHeight + lineHeight * 10);
            drawString(g, "Automatic Transmission", 8, imageHeight + lineHeight * 11);
            drawString(g, "Beverage Holders", 8, imageHeight + lineHeight * 12);
            drawString(g, "Bucket Sets", 8, imageHeight + lineHeight * 13);
            drawString(g, "Center Arm Rest/Console", 8, imageHeight + lineHeight * 14);
            drawString(g, "Child Proof Locks", 8, imageHeight + lineHeight * 15);
            drawString(g, "CD Player", 8, imageHeight + lineHeight * 16);
            drawString(g, "Cruise Control", 8, imageHeight + lineHeight * 17);
            drawString(g, "Factory Warranty", 8, imageHeight + lineHeight * 18);
            drawString(g, "Radio", 8, imageHeight + lineHeight * 19);

            ScrollHeight = imageHeight + lineHeight * 21;
        }

        private void drawString(Graphics g, string s, int left, int top)
        {
            g.DrawString(s, Common.Values.Font, Common.Values.FontBrush, Rectangle.Left + left * ScreenFactor, Rectangle.Top + ScrollTop + top);
        }

        public override void MouseUp(Point p)
        {
            base.MouseUp(p);

            if(HorizontalGesture != 0 && p.Y < imageHeight)
            {
                if(HorizontalGesture > 0 && currentImage < 2 && images[currentImage + 1] != null)
                {
                    animationOffset = Rectangle.Width / 4;
                    animationLeft = Rectangle.Width;
                    currentImage++;
                }
                else if(HorizontalGesture < 0 && currentImage > 0 && images[currentImage - 1] != null)
                {
                    animationOffset = -Rectangle.Width / 4;
                    animationLeft = -Rectangle.Width;
                    currentImage--;
                }
            }
        }

        public override void KeyUp(KeyEventArgs e)
        {
            base.KeyUp(e);

            if(e.KeyCode == Keys.Up)
            {
                ScrollTop += Rectangle.Height / 4;
                if(ScrollTop > 0)
                    ScrollTop = 0;
            }
            else if(e.KeyCode == Keys.Down)
            {
                ScrollTop -= Rectangle.Height / 4;
                if(ScrollTop < Rectangle.Height - Rectangle.Top - ScrollHeight)
                    ScrollTop = Rectangle.Height - Rectangle.Top - ScrollHeight;
            }
            else if(e.KeyCode == Keys.Right && currentImage < 2 && images[currentImage + 1] != null)
            {
                animationOffset = Rectangle.Width / 4;
                animationLeft = Rectangle.Width;
                currentImage++;
            }
            else if(e.KeyCode == Keys.Left && currentImage > 0 && images[currentImage - 1] != null)
            {
                animationOffset = -Rectangle.Width / 4;
                animationLeft = -Rectangle.Width;
                currentImage--;
            }
            e.Handled = true;
        }
    }
}
